/**
 * 
 */
package com.hfjyz.examination.service.impl;

import java.util.Calendar;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import com.hfjyz.examination.command.CategoryEditCommand;
import com.hfjyz.examination.dao.QuestionCategoryDao;
import com.hfjyz.examination.domain.QuestionCategory;
import com.hfjyz.examination.exception.HfjyzException;
import com.hfjyz.examination.service.CategoryService;

/**
 *
 */
public class CategoryServiceImpl implements CategoryService {

	private QuestionCategoryDao questionCategoryDao;

	@Override
	public void editOk(HttpServletRequest request, CategoryEditCommand command,
			BindException errors) throws Exception {
		QuestionCategory category = null;
		if (command.getId() != null) {
			category = questionCategoryDao.get(command.getId());
			if (category == null) {
				throw new HfjyzException("error.category.edit.alreadydelete");
			}
		} else {
			category = new QuestionCategory();
		}
		category.setName(command.getName());
		category.setRemark(command.getRemark());
		category.setStatus(command.getStatus());
		// category.setAdminid();
		category.setCdate(Calendar.getInstance().getTime());
		if (command.getId() == null) {
			Long pk = questionCategoryDao.add(category);
			command.setId(pk);
		} else {
			questionCategoryDao.update(category);
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map initEdit(HttpServletRequest request,
			CategoryEditCommand command, Errors errors) throws Exception {
		if (command.getId() != null) {
			QuestionCategory category = questionCategoryDao
					.get(command.getId());
			command.setCategory(category);
		}
		return null;
	}

	/**
	 * @return the questionCategoryDao
	 */
	public QuestionCategoryDao getQuestionCategoryDao() {
		return questionCategoryDao;
	}

	/**
	 * @param questionCategoryDao
	 *            the questionCategoryDao to set
	 */
	public void setQuestionCategoryDao(QuestionCategoryDao dao) {
		this.questionCategoryDao = dao;
	}

}
